package com.douwong.student.utils;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.Config;

import java.util.List;
import java.util.UUID;


public class UIUtils {
	/**
	 * 发送自定广播
	 * @param context
	 * @param broadcastType 广播类型
     */
	public static void sendBroadcast(Context context,String broadcastType){
		sendBroadcast(context,broadcastType,null);
	}
	public static  void  sendBroadcast(Context context,String broadcastType,String  parcelableData){
		Intent intent=new Intent();
		intent.putExtra(Config.INTENT_DATA,parcelableData);
		intent.setAction(broadcastType);
		context.sendBroadcast(intent);
	}
	public static Context getContext() {
		return BaseApplication.mContext;
	}

	/**
	 * 获取uuid
	 * @return
	 */
	public static String getUUidString(){
	return UUID.randomUUID().toString();
}

	/** dip转换px */
	public static int dip2px(int dip) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}


	/** pxz转换dip */
	public static int px2dip(int px) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

	/** inflate打气�?*/
	public static View inflate(int resId) {
		return LayoutInflater.from(getContext()).inflate(resId, null);
	}
	
	/**  当使用了自定义样式时使用此方法，context应传Activity.context  */
	public static View inflate(Context context,int resId){
		return LayoutInflater.from(context).inflate(resId, null);
	}

	public static View inflate(Context context,int resId,ViewGroup root,boolean attachToRoot){
		return LayoutInflater.from(context).inflate(resId,root,attachToRoot);
	}

	/** 获取资源 */
	public static Resources getResources() {

		return getContext().getResources();
	}

	/** 获取文字 */
	public static String getString(int resId) {
		return getResources().getString(resId);
	}

	/** 获取文字数组 */
	public static String[] getStringArray(int resId) {
		return getResources().getStringArray(resId);
	}

	/** 获取dimen */
	public static int getDimens(int resId) {
		return getResources().getDimensionPixelSize(resId);
	}

	/** 获取drawable */
	public static Drawable getDrawable(int resId) {
		return getResources().getDrawable(resId);
	}

	/** 获取颜色 */
	public static int getColor(int resId) {
		return getResources().getColor(resId);
	}

	/** 获取颜色选择�?*/
	public static ColorStateList getColorStateList(int resId) {
		return getResources().getColorStateList(resId);
	}




	/**
	 * view转bitmap
	 *
	 * @param addViewContent
	 * @return
	 */
	public static Bitmap getViewBitmap(View addViewContent) {

		addViewContent.setDrawingCacheEnabled(true);

		addViewContent.measure(
				View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
				View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		addViewContent.layout(0, 0,
				addViewContent.getMeasuredWidth(),
				addViewContent.getMeasuredHeight());

		addViewContent.buildDrawingCache();
		Bitmap cacheBitmap = addViewContent.getDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

		return bitmap;
	}
	/**
	 * 包名获取图标
	 * @param packageName
	 * @return
	 */
	public static Drawable getAppIcon(String packageName){
		//		Log.d(TAG, "-------------->packageName :"+packageName);
		PackageManager pm = UIUtils.getContext().getPackageManager();
		List<PackageInfo> packages = pm.getInstalledPackages(0);
		for (int i = 0; i < packages.size(); i++) {
			PackageInfo packageInfo = packages.get(i);
			if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {

				if(packageInfo.packageName.equals(packageName)){

					return packageInfo.applicationInfo.loadIcon(pm);
				}
			}

		}
		return null;
	}

	// 判断当前的线程是不是在主线程
	public static boolean isRunInMainThread() {
		return android.os.Process.myTid() == BaseApplication.mMainThreadId;
	}
	/** 在主线程执行runnable */
	public static boolean post(Runnable runnable) {
		return BaseApplication.mHandler.post(runnable);
	}

	public static void runInMainThread(Runnable runnable) {
		// 在主线程运行
		if (isRunInMainThread()) {
			runnable.run();
		} else {
			post(runnable);
		}
	}
}
